package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.AdminAuthCheck;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.dto.user.UserAddDTO;
import com.aloda.dto.user.UserUpdateDTO;
import com.aloda.query.UserPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.UserService;
import com.aloda.vo.user.UserOnlineVO;
import com.aloda.vo.user.UserPageVO;
import com.aloda.vo.user.UserSafetyVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UserController
 * @author aloda
 * @date 2024/12/19
 */
@Slf4j
@RequestMapping("/admin/user")
@RestController("admin")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询
     * @param userPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<UserPageVO>> pageQuery(@RequestBody UserPageDTO userPageDTO) {
        PageResult<UserPageVO> pageResult = userService.pageQuery(userPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserSafetyVO> getById(@PathVariable Long id) {
        UserSafetyVO userSafetyVO = BeanUtil.copyProperties(userService.getById(id), UserSafetyVO.class);
        return Result.success(userSafetyVO);
    }

    /**
     * 新增用户
     * @param userAddDTO
     * @return
     */
    @OperateLogAnno(category = "新增用户")
    @PostMapping
    public Result addUser(@RequestBody UserAddDTO userAddDTO) {
        return Result.success(userService.addUser(userAddDTO));
    }

    /**
     * 编辑用户
     * @param userUpdateDTO
     * @return
     */
    @OperateLogAnno(category = "编辑用户")
    @AdminAuthCheck
    @PutMapping
    public Result editUser(@RequestBody UserUpdateDTO userUpdateDTO) {
        return Result.success(userService.editUser(userUpdateDTO));
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @OperateLogAnno(category = "删除用户")
    @AdminAuthCheck
    @DeleteMapping
    public Result deleteUser(@RequestBody List<Long> ids) {
        return Result.success(userService.removeBatchByIds(ids));
    }


    /**
     * 重置密码
     * @param id
     * @return
     */
    @OperateLogAnno(category = "重置密码")
    @AdminAuthCheck
    @PutMapping("/password/{id}")
    public Result resetPassword(@PathVariable Long id) {
        userService.resetPw(id);
        return Result.success();
    }

    /**
     * 获取在线用户
     * @return
     */
    @AdminAuthCheck
    @GetMapping("/online")
    public Result<List<UserOnlineVO>> getOnlineUser() {
        List<UserOnlineVO> list = userService.getOnlineUser();
        return Result.success(list);
    }

    /**
     * 强制下线用户
     * @param token
     * @return
     */
    @AdminAuthCheck
    @OperateLogAnno(category = "强制下线用户")
    @PostMapping("/offline")
    public Result offlineUser(@RequestParam String token) {
        log.info("强制下线用户：{}",token);
        return Result.success(userService.offlineUser(token));
    }

    /**
     * 设置用户状态
     * @param status
     * @return
     */
    @OperateLogAnno(category = "设置用户状态")
    @PostMapping("/status/{id}")
    public Result setUserStatus(@RequestParam Integer status, @PathVariable Long id) {
        userService.setUserStatus(status, id);
        return Result.success();
    }

    /**
     * 获取用户总数量
     * @return
     */
    @GetMapping("/nums")
    public Result getUserNums() {
        return Result.success(userService.count());
    }

    /**
     * 导出用户表格
     *
     * @param response HttpServletResponse
     * @param ids      用户ID列表
     */
    @AdminAuthCheck
    @OperateLogAnno(category = "导出用户表格")
    @PostMapping("/export")
    public void exportUserToExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        userService.exportUserToExcel(response, ids);
    }
}
